package com.ysu.demo.controller;

import com.ysu.demo.VO.ResultVO;
import com.ysu.demo.annotation.PassToken;
import com.ysu.demo.dto.UserDTOToken;
import com.ysu.demo.enums.ExceptionEnum;
import com.ysu.demo.exception.BaseException;
import com.ysu.demo.from.LoginFrom;
import com.ysu.demo.service.LoginService;
import com.ysu.demo.utlis.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：lhw
 * @date ：Created in 2019/8/9 10:02
 */
@RestController
public class BaseControllers {
    @Autowired
    LoginService loginService;

    @GetMapping("/ping")
    @PassToken
    public ResultVO ping(){
        return ResultUtil.TrueResult("112121");
    }


    @GetMapping("/ex")
    @PassToken
    public ResultVO exception(){
        throw new BaseException(ExceptionEnum.NOT_FIND);
    }

    @PostMapping("/login")
    @PassToken
    public ResultVO login(@RequestBody LoginFrom loginFrom,
                          BindingResult bindingResult) throws Throwable {
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        return ResultUtil.TrueResult(loginService.login(loginFrom));
    }
}
